<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="骑手ID" prop="riderId">
        <el-input
          v-model="queryParams.riderId"
          placeholder="请输入骑手ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店ID" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入门店ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="系统/门店分配时间" prop="assignTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.assignTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择系统/门店分配时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="取货时间" prop="pickupTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.pickupTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择取货时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="送达时间" prop="deliveryTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.deliveryTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择送达时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="配送状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择配送状态" clearable style="width: 200px" >
          <el-option
            v-for="dict in rider_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="取消原因" prop="cancelReason">
        <el-input
          v-model="queryParams.cancelReason"
          placeholder="请输入取消原因"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="骑手备注信息" prop="riderNotes">-->
<!--        <el-input-->
<!--          v-model="queryParams.riderNotes"-->
<!--          placeholder="请输入骑手备注信息"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="createdAt">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.createdAt"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择创建时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updatedAt">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.updatedAt"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择更新时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['transaction:riderorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['transaction:riderorder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['transaction:riderorder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['transaction:riderorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riderorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配送记录ID" align="center" prop="id" />
<!--      <el-table-column label="骑手ID" align="center" prop="riderId" />-->
<!--      <el-table-column label="门店ID" align="center" prop="storeId" />-->
<!--      <el-table-column label="订单ID" align="center" prop="orderId" />-->
      <el-table-column label="骑手ID" align="center">
        <template #default="scope">
          {{ scope.row.riderId }} - {{ scope.row.rider ? scope.row.rider.name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="所属门店" align="center">
        <template #default="scope">
          {{ scope.row.storeId }} - {{ scope.row.storeInfo ? scope.row.storeInfo.name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="订单" align="center" >
        <template #default="scope">
          {{ scope.row.orderId }} - {{ scope.row.order ? scope.row.order.orderNo : '' }}
        </template>
      </el-table-column>

      <el-table-column label="系统/门店分配时间" align="center" prop="assignTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.assignTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取货时间" align="center" prop="pickupTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.pickupTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="送达时间" align="center" prop="deliveryTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="送达凭证图片URL" align="center" prop="deliveryProofUrl" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.deliveryProofUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="配送状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="rider_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="骑手备注信息" align="center" prop="riderNotes" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['transaction:riderorder:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['transaction:riderorder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改骑手订单配送记录对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="riderorderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="骑手ID" prop="riderId">
          <el-input v-model="form.riderId" placeholder="请输入骑手ID" />
        </el-form-item>
        <el-form-item label="门店ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入门店ID" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="系统/门店分配时间" prop="assignTime">
          <el-date-picker clearable
            v-model="form.assignTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择系统/门店分配时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="取货时间" prop="pickupTime">
          <el-date-picker clearable
            v-model="form.pickupTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择取货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="送达时间" prop="deliveryTime">
          <el-date-picker clearable
            v-model="form.deliveryTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择送达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="送达凭证图片URL" prop="deliveryProofUrl">
          <image-upload v-model="form.deliveryProofUrl"/>
        </el-form-item>
        <el-form-item label="配送状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择配送状态">
            <el-option
              v-for="dict in rider_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="取消原因" prop="cancelReason">
          <el-input v-model="form.cancelReason" placeholder="请输入取消原因" />
        </el-form-item>
        <el-form-item label="骑手备注信息" prop="riderNotes">
          <el-input v-model="form.riderNotes" placeholder="请输入骑手备注信息" />
        </el-form-item>
        <el-form-item label="逻辑删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除标志" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Riderorder">
import { listRiderorder, getRiderorder, delRiderorder, addRiderorder, updateRiderorder } from "@/api/transaction/riderorder"

const { proxy } = getCurrentInstance()
const { rider_status } = proxy.useDict('rider_status')

const riderorderList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    riderId: null,
    storeId: null,
    orderId: null,
    assignTime: null,
    pickupTime: null,
    deliveryTime: null,
    deliveryProofUrl: null,
    status: null,
    cancelReason: null,
    riderNotes: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    riderId: [
      { required: true, message: "骑手ID不能为空", trigger: "blur" }
    ],
    storeId: [
      { required: true, message: "门店ID不能为空", trigger: "blur" }
    ],
    orderId: [
      { required: true, message: "订单ID不能为空", trigger: "blur" }
    ],
    assignTime: [
      { required: true, message: "系统/门店分配时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "配送状态不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询骑手订单配送记录列表 */
function getList() {
  loading.value = true
  listRiderorder(queryParams.value).then(response => {
    riderorderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    riderId: null,
    storeId: null,
    orderId: null,
    assignTime: null,
    pickupTime: null,
    deliveryTime: null,
    deliveryProofUrl: null,
    status: null,
    cancelReason: null,
    riderNotes: null,
    delFlag: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("riderorderRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加骑手订单配送记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRiderorder(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改骑手订单配送记录"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["riderorderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRiderorder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRiderorder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// /** 删除按钮操作 */
// function handleDelete(row) {
//   const _ids = row.id || ids.value
//   proxy.$modal.confirm('是否确认删除骑手订单配送记录编号为"' + _ids + '"的数据项？').then(function() {
//     return delRiderorder(_ids)
//   }).then(() => {
//     getList()
//     proxy.$modal.msgSuccess("删除成功")
//   }).catch(() => {})
// }

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认逻辑删除骑手订单配送记录编号为"' + _ids + '"的数据项？').then(function() {
    return delRiderorder(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('transaction/riderorder/export', {
    ...queryParams.value
  }, `riderorder_${new Date().getTime()}.xlsx`)
}

getList()
</script>
